package com.kantboot.business.bp.repository;

import com.kantboot.business.bp.domain.dto.BusBpProblemSearchDTO;
import com.kantboot.business.bp.domain.entity.BusBpProblem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface BusBpProblemRepository extends JpaRepository<BusBpProblem, Long> {

    @Query("""
            SELECT p
            FROM BusBpProblem p
            WHERE
            (:#{#param.gradeId} IS NULL OR p.gradeId = :#{#param.gradeId})
            AND (:#{#param.subjectId} IS NULL OR p.subjectId = :#{#param.subjectId})
            AND (:#{#param.keyword} IS NULL OR p.text LIKE CONCAT('%',:#{#param.keyword},'%'))
            ORDER BY p.id DESC
            """)
    List<BusBpProblem> getBodyDataFront(@Param("param")BusBpProblemSearchDTO dto);


    List<BusBpProblem> findByUserId(Long userId);

    @Query("""
            SELECT p
            FROM BusBpProblem p
            WHERE
            (:#{#param.gradeId} IS NULL OR p.gradeId = :#{#param.gradeId})
            AND (:#{#param.subjectId} IS NULL OR p.subjectId = :#{#param.subjectId})
            AND (:#{#param.keyword} IS NULL OR p.text LIKE CONCAT('%',:#{#param.keyword},'%'))
            ORDER BY p.id DESC
            """)
    Page<BusBpProblem> getBodyData(@Param("param")BusBpProblemSearchDTO dto, Pageable pageable);
}
